package domain;

import java.util.Scanner;

public class TwoPlayerGame {

	Card c;
	private int p1Score= 0, p2Score= 0, pairCount= 0;
	private int r1= 0,c1= 0,r2= 0,c2= 0;
	private boolean isEqual = true;
	private String player1, player2;
	
	public void playGame(String player1, String player2) {
		this.player1 = player1;
		this.player2 = player2;
		
		Board board=new Board();
		board.initBoard(2,2);
	
		Scanner in = new Scanner(System.in);
		while(pairCount!=2){
		while(isEqual){
			try{
				System.out.println("First player's turn!");
				System.out.println("enter first row then column between 0 and 3");
	
				// Reads a single line from console 
				// and stores into name variable
				r1 = in.nextInt();
				c1 = in.nextInt();
				r2 = in.nextInt();
				c2 = in.nextInt();
	
				// the variables that is taken from console determine position of two cards on board
				Card a = board.getBoard(r1, c1);
				Card b = board.getBoard(r2, c2);
	
				if(a==b){
					System.out.println("correct!");
					p1Score ++;
					pairCount ++;
					if(pairCount == 2){
						System.out.println("game over!");
						printResult();
						break;
					}
				}else{
					System.out.println("wrong, player2's turn");
					isEqual = false;
				}
			}
			catch(ArrayIndexOutOfBoundsException e){
				System.out.println("Please give a number between 0 and 3!");
			}
		}
		
		while(!isEqual){
			try{
				System.out.println("Second player's turn!");
				System.out.println("enter first row then column between 0 and 3");
	
				// Reads a single line from console 
				// and stores into name variable
				r1 = in.nextInt();
				c1 = in.nextInt();
				r2 = in.nextInt();
				c2 = in.nextInt();
	
				// the variables that is taken from console determine position of two cards on board
				Card a = board.getBoard(r1, c1);
				Card b = board.getBoard(r2, c2);
	
				if(a==b){
					System.out.println("correct!");
					p2Score ++;
					pairCount ++;
					if(pairCount == 2){
						System.out.println("game over!");
						printResult();
						break;
					}
					
				}else{
					System.out.println("wrong, player1's turn");
					isEqual = true;
				}
			}
			catch(ArrayIndexOutOfBoundsException e){
				System.out.println("Please give a number between 0 and 3!");
			}
		}
		
		
		}
		in.close();
	}

	public void printResult(){
		if(p1Score > p2Score) System.out.printf("%s wins! Scores are %d and %d", player1, p1Score, p2Score);
		else System.out.printf("%s wins! Scores are %d and %d", player2, p2Score, p1Score);
	}

}
